package util;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;

public class Comm {
    private static String driver = "com.mysql.cj.jdbc.Driver";
    private static String url;
    private static String user = "root";
    private static String password = "123456";
    private static String dbname = "lianxi";

    private Connection conn;
    public Comm() throws Exception{
        Init();
    }

    public Comm Init() throws Exception  {
        Class.forName(driver);
        url = "jdbc:mysql://localhost:3306/" + dbname + "?user=" + user + "&password=" + password
                + "&useUnicode=true&characterEncoding=UTF-8";
        conn = DriverManager.getConnection(url);
        return this;
    }

    public Comm Close() throws Exception {
        if (conn != null) {
            conn.close();
        }
        return this;
    }

    public Comm Exec(String sql) throws Exception {
        PreparedStatement ps = conn.prepareStatement(sql);
        ps.execute();
        return this;
    }

    
    public ResultSet Query(String sql) throws Exception  {
        Statement st = conn.createStatement();
        ResultSet rs = st.executeQuery(sql);
        return rs;
    }
}